package com.demo.threadStop;

/**
 * 通过通知的方式 控制线程终止
 * @author gy
 * @date 2022/4/23
 */
public class ThreadStop {
    public static void main(String[] args) {
        T1 t1 = new T1();
        Thread thread = new Thread(t1);
        thread.start();
        System.out.println("main线程休眠10秒钟");
        try {
            Thread.sleep(10*1000);
            t1.setLoop(false);
            System.out.println("t1 线程停止");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class T1 implements Runnable{

    private int count;

    private boolean loop = true;

    @Override
    public void run() {
        while (loop){
            try {
                System.out.println(++count);
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public void setLoop(boolean loop) {
        this.loop = loop;
    }
}